(function($) {
    $.switchable = function(options) {
        var settings = $.extend({
            "nowClass": "now",
            "hover": true,
            "anim": "none",
            "duration": 400,
            "onSwitch": function(){}
        }, options);
        return new Switch(settings);
    }
    
    /**
     * tab切换处理类
     */
    function Switch(settings) {
        this.tabs = $(settings["tab"]);
        this.boxs = $(settings["box"]);
        this.nowClass = settings["nowClass"];
        this.hover = settings["hover"]; //悬停时是否切换
        this.anim = settings["anim"]; //动画效果
        this.duration = settings["duration"]; //动画的时间
        this.onSwitch = settings["onSwitch"]; //切换时的回调函数
        this.index = 0;
        var _this = this;
        var tabTimer;
        
        if (this[this.anim + "AnimInit"]) {
            this[this.anim + "AnimInit"]();
        }
        
        if (this.hover === true) {
            this.tabs.hover(function() {
                var tab = $(this);
                tabTimer = setTimeout(function() {
                    _this.dealSwitch(tab);
                }, 200);
            }, function() {
                clearTimeout(tabTimer);
            });
        }
        this.tabs.click(function() {
            var tab = $(this);
            clearTimeout(tabTimer);
            _this.dealSwitch(tab);
        });
    }
    
    $.extend(Switch.prototype, {
        /**
         * 处理切换
         * @param {Object} tab 需要切换到的tab
         */
        dealSwitch: function(tab) {
            this.tabs.removeClass(this.nowClass);
            tab.addClass(this.nowClass);
            this.index = this.tabs.index(tab);
            this[this.anim + "AnimRender"](this.index);
            this.onSwitch(this.index);
        },
        
        /**
         * 无动画效果的切换
         * @param {Object} index
         */
        noneAnimRender: function(index) {
            this.boxs.hide();
            this.boxs.eq(index).show();
        },
        
        /**
         * 垂直滚动动画所需的初始化
         */
        scrollVAnimInit: function() {
            this.scrollContainer = $("<div></div>").css("position", "absolute");
            this.boxs.parent().css("position", "relative")
            this.boxs.parent().append(this.scrollContainer.append(this.boxs));
            this.boxHeight = this.boxs.eq(0).innerHeight();
        },
        
        /**
         * 垂直滚动的切换动画
         * @param {Object} index 需要展示的box的index
         */
        scrollVAnimRender: function(index) {
            var newTop = (index) * -(this.boxHeight);
            this.scrollContainer.stop().animate({
                top: newTop
            }, this.duration);
        },
        
        /**
         * 水平滚动动画所需的初始化
         */
        scrollHAnimInit: function() {
            this.scrollContainer = $("<div></div>").css({
                "position": "absolute",
                "width": "10000px"
            });
            this.boxs.parent().css("position", "relative");
            this.boxs.parent().append(this.scrollContainer.append(this.boxs));
            this.boxWidth = this.boxs.eq(0).innerWidth();
        },
        
        /**
         * 水平滚动的切换动画
         * @param {Object} index 需要展示的box的index
         */
        scrollHAnimRender: function(index) {
            var newLeft = (index) * -(this.boxWidth);
            this.scrollContainer.stop().animate({
                left: newLeft
            }, this.duration);
        }
    });
    
})(jQuery);